package com.nervenets.general.hibernate.type;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nervenets.general.utils.JsonUtils;
import com.vladmihalcea.hibernate.type.AbstractHibernateType;
import com.vladmihalcea.hibernate.type.json.internal.JsonTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;
import org.hibernate.usertype.DynamicParameterizedType;

import java.lang.reflect.Type;
import java.util.Properties;

/**
 * Created by joe on 2024/3/1.
 */
public class HibernateJsonStringType extends AbstractHibernateType<Object> implements DynamicParameterizedType {
    public static final HibernateJsonStringType INSTANCE = new HibernateJsonStringType();

    public HibernateJsonStringType() {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper()));
    }

    public HibernateJsonStringType(Type javaType) {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper(), javaType));
    }

    public HibernateJsonStringType(Configuration configuration) {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper()), configuration);
    }

    public HibernateJsonStringType(ObjectMapper objectMapper) {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper()));
    }

    public HibernateJsonStringType(ObjectMapperWrapper objectMapperWrapper) {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper()));
    }

    public HibernateJsonStringType(ObjectMapper objectMapper, Type javaType) {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper(), javaType));
    }

    public HibernateJsonStringType(ObjectMapperWrapper objectMapperWrapper, Type javaType) {
        super(HibernateJsonStringSqlTypeDescriptor.INSTANCE, new JsonTypeDescriptor(JsonUtils.longToStringObjectMapperWrapper(), javaType));
    }

    public String getName() {
        return "json";
    }

    protected boolean registerUnderJavaType() {
        return true;
    }

    public void setParameterValues(Properties parameters) {
        ((JsonTypeDescriptor) this.getJavaTypeDescriptor()).setParameterValues(parameters);
    }
}
